<?php

namespace Home\Controller;

use Think\Controller;
use Think\Model;
use Think\Page;
use Think\Upload;

class IndexController extends Controller
{

    public function index()
    {
        $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>', 'utf-8');
    }

    public function blog()
    {

//        $this->assign('list','mylist');
        $data = M('blog');
        $this->blogs = $data->select();
        $this->display();
    }

    public function add()
    {
        $this->display();
    }

    public function addblog1()
    {

        $article = M('Article');                            //建立模型对象
//        $f = $article->getLastSql();
//        var_dump($f);
        $data = $article->create();                         //准备数据
        $data['edit_at'] = time();                          //添加时间项目
        $res = $article->add($data);                       //添加
        if ($res) {
            $this->success('添加成功', '', 2);
        } else {
            $this->error('添加失败');
        }

    }

    public function addblog()
    {
        $article = D('Article');
        $article->create();
        $res = $article->add();
        if ($res) {
            $this->success('添加成功', U("main"));
        } else {
            $this->error($article->getError());
        }
        $this->upload();
    }

    protected function upload()
    {

        $config = array('maxSize' => 3145728,
            'savePath' => './Public/Uploads/image/',
            'exts' => array('jpg', 'gif', 'png', 'jpeg'),
            'autoSub' => true, 'subName' => array('date', 'Ymd'),
        );
        $upload = new Upload($config);
        $upload = M('Article');

        $info = $upload->uploadOne($_FILES['url']);
        $data['photo'] = $info[0]['savePath'];
        $upload->add($data);


    }

    public function main()
    {
        $article = M('Article');
        $p = I('get.page');                    //获得当前页数
        $totlepage = $article->count();     //统计总记录数
        $this->pager = new page($totlepage, 3);
        $data = $article->limit($p, 3)->select();
        $this->assign('lists', $data);
        $this->display();

    }

    public function myread()
    {
        $article = M('Article');
        $id = $_GET['id'];
        $data = $article->find($id);
        if ($data) {
            $this->assign('blog', $data);


        } else {
            $this->error('数据没找到');
        }
        $article->where("id=$id")->setInc('read_times');

        $this->display();

    }

    public function modify()
    {
        $article = M('Article');
        $id = $_GET['id'];
        $data = $article->find($id);
        if ($data) {
            $this->assign('blog', $data);
            $this->display();

        } else {
            $this->error('数据没找到');
        }

    }

    public function modifysave()
    {
        $article = M('Article');
        $id = $_GET['id'];
        $data = $article->create();
        if ($data) {
            $article->save($id);

        } else {
            $this->error('错误');
        }
    }

    public function delete()
    {
        $article = M('Article');
        $id = $_GET['id'];
        if ($id) {
            $article->delete($id);
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    public function _before_register()
    {
        if (IS_POST) {
            $u = D('User');
            $data = $u->create();
            if ($data) {
                $pa2 = $_POST['password2'];
                $pa = $_POST['password'];
                $email = $_POST['email'];
                //用户名在不在
                $c['username'] = $data['username'];
                if ($u->where($c)->count()) {
                    $this->error("用户名已存在");
                }
                var_dump($pa);
                var_dump($pa2);
                if ($pa == $pa2) {
                    //密码加密
                    $data['password'] = md5($data['password']);
                    $res = $u->add($data);

                } else {
                    $this->error('两次密码输入不匹配');
                }
                $this->success("注册成功", U("login"));
                exit;
            } else {
                $this->error($u->getError());
            }
        }
    }

    public function register()
    {
        $this->display();
    }

    public function checkUserAjax($name)
    {

        $m = M('User');
        $c['username'] = $name;
        $num = $m->where($c)->count();
//        echo $m->getLastSql();
        if ($num > 0) {
            $r['status'] = 0;
            $r['msg'] = '用户名已存在，不能注册';
        } else {
            $r['status'] = 1;
            $r['msg'] = '可以注册';
        }
//        echo json_encode($r);
        $this->ajaxReturn($r);

    }

    public function _before_login()
    {
        if (IS_POST) {
            $u = M('User');
            $data = $u->create();
            $data['password'] = md5($data['password']);
            $userArr = $u->where($data)->find();
            if ($userArr) {
                //写凭证
                session('user_id', $userArr['id']);
                //提示
                $this->success('登录成功', U('main'));
            } else {
                //提示用户名密码错误
                $this->error('密码错误');

            }
            exit;
        }
    }

    public function login()
    {
        $this->display();
    }


    public function loginOut()
    {
        session('user_id', null);
        $this->success('退出成功，返回到首页', U('Home/Index/index'));
    }
}